<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/3 0003
 * Time: 下午 5:28
 */

namespace Admin\Controller;


class TableController extends PublicController
{

    function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 桌位管理
     * Author:边
     */
    public function index()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $map = '';
        } else {
            $map['p.store_id'] = $store_id['store_id'];
        }

        $count = M('dishes_people')->alias("p")->where($map)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 12);// 实例化分页类 传入总记录数和每页显示的记录数(25)
        $Page->setConfig('theme', "<ul class='pagination no-margin pull-right'></li><li>%FIRST%</li><li>%UP_PAGE%</li><li>%LINK_PAGE%</li><li>%DOWN_PAGE%</li><li>%END%</li><li><a> %HEADER%  %NOW_PAGE%/%TOTAL_PAGE% 页</a></ul>");
        $show = $Page->show(); // 分页显示输出
        $result = M('dishes_people')->alias("p")->join("meal_store as s on p.store_id=s.store_id")->field("p.*,s.store_name")->where($map)->limit($Page->firstRow . ',' . $Page->listRows)->order("p.id desc")->select();
        foreach ($result as $key => $v) {
            if ($v['create_time']) {
                $result[$key]['create_formart_time'] = date('Y-m-d', $v['create_time']);
            } else {
                $result[$key]['create_formart_time'] = '';
            }
            switch ($v['model']) {
                case 1:
                    $result[$key]['formart_model'] = '小桌';
                    break;
                case 2:
                    $result[$key]['formart_model'] = '中桌';
                    break;
                case 3:
                    $result[$key]['formart_model'] = '大桌';
                    break;
                default : $result[$key]['formart_model'] = '小桌';
            }
        }
        $this->assign("page", $show); // 赋值分页输出
        $this->assign("result", $result);
        $this->display();
    }

    /**
     *桌位增加修改加载页面
     * Author:边
     */
    public function add_edit()
    {
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $map = '';
            $storeId = 0;
        } else {
            $map['store_id'] = $store_id['store_id'];
            $storeId = $store_id['store_id'];
        }

        $id = I('get.id');
        $map['id'] = $id;
        $table = M('dishes_people')->where($map)->find();
        $store = M('store')->select();
        $this->assign('result', $table);
        $this->assign('store', $store);
        $this->assign('storeId', $storeId);
        $this->display();
    }

    /**
     * 增加修改桌位
     * Author:边
     */
    public function editTable()
    {
        $data['qrcode'] = I('post.qcrode');
        $id = I('post.id');
        $data['table_id'] = I('post.table_id');
        $data['num'] = I('post.num');
        $data['model'] = I('post.model');
        $store_id = session("wadmin");
        if ($store_id['store_id']==0) {
            $data['store_id'] = I("post.store_id"); //获取的店铺ID
        } else {
            $data['store_id'] = $store_id['store_id']; //储存的店铺ID
        }

        if ($id) {
            $data['qrcode'] = I('post.qcrode');
            $result = M('dishes_people')->where("id='$id'")->save($data);
        } else {
            $data['creater'] = $store_id['user_name'];
            $data['create_time'] = time();
            $data['qrcode'] = I('post.qcrode');
            $result = M('dishes_people')->add($data);
        }
        if ($result) {
            $this->success("操作成功", U('Admin/table/index'));
        } else {
            $this->error("操作失败");
        }
    }

    /**
     * 删除桌位
     * Author:边
     */
    public function delTable(){
        $id = I('get.id');
        $result=M('dishes_people')->where("id='$id'")->delete();
        if($result){
            $this->success("操作成功",U('Admin/table/index'));
        }else{
            $this->error("操作失败",U('Admin/table/index'));
        }
    }

    /**
     * 桌位二维码
     */
//    public function qrcode()
//    {
//        $id = I('request.id');
//        $store_id = I('request.store_id');
//
//        $value = "http://mytest.bndvip.com/html/meal_new/index.html?table=" .$id."&store_id=".$store_id;//二维码内容
//        Vendor('phpqrcode.phpqrcode');
//        //生成二维码图片
//        $object = new \QRcode();
//        $level = 3;
//        $size = 4;
//        $errorCorrectionLevel = intval($level);//容错级别
//        $matrixPointSize = intval($size);//生成图片大小
//        $object->png($value, false, $errorCorrectionLevel, $matrixPointSize, 2);
//    }

    public function qrcode()
    {
        $table_id = I('request.table_id');//桌号
        $store_id = I('request.store_id');//店铺id
//        $value = "http://mytest.bndvip.com/html/newMeal/index.html?table=" .$id."&store_id=".$store_id;//二维码内容
        $value = "http://mytest.bndvip.com/index.php/Home/index/index?table_id=" .$table_id."&store_id=".$store_id;//二维码内容
        Vendor('phpqrcode.phpqrcode');
        //生成二维码图片
        $object = new \QRcode();
        $level = 3;
        $size = 4;
        $errorCorrectionLevel = intval($level);//容错级别
        $matrixPointSize = intval($size);//生成图片大小
        $object->png($value, false, $errorCorrectionLevel, $matrixPointSize, 2);
    }
}